/**
 * Created by eva on 2019/12/29.
 */
function Fruit() {
    this.alive = [];
    this.x = [];
    this.y = [];
    this.l = [];
    this.spd = [];
    this.fruitType = [];
    this.num = 30;
    this.orange = new Image();
    this.blue = new Image();
    this.aneid = [];
}
Fruit.prototype = {
    init : function () {

        for(var i = 0; i < this.num; i++){
            this.alive[i] = false;
            this.x[i] = 0;
            this.y[i] = 0;
            this.spd[i] = Math.random()*0.013+0.003;
            this.born(i);
        }
        this.orange.src = "./src/fruit.png";
        this.blue.src = "./src/blue.png";
    },
    draw : function () {
        for(var i = 0; i < this.num; i++){
            if(this.alive[i]){
                if(this.l[i] <= 14){
                    this.l[i] += this.spd[i]*deltaTime;
                    var id = this.aneid[i];
                    this.x[i] = ane.headx[id];
                    this.y[i] = ane.heady[id];
                }else{
                    this.y[i] -= 7*this.spd[i]*deltaTime;
                }

                ctx2.drawImage(this[this.fruitType[i]],this.x[i] - this.l[i]*0.5,this.y[i] - this.l[i]*0.5,this.l[i],this.l[i]);
                if(this.y[i] < 10){
                    this.alive[i] = false;
                }
            }

        }
    },
    born : function (i) {
        var id = Math.floor(Math.random()*ane.num);
        this.aneid[i] = id;
        // this.x[i] = ane.headx[id];
        // this.y[i]= ane.heady[id];
        this.l[i] = 0;
        this.alive[i] = true;
        var ran = Math.random();
        this.fruitType[i] = ran < 0.2 ? "blue" : "orange";
    },
    update : function () {
        var num = 0;
        for(var i = 0; i < this.num; i++){

        }
    },
    fruitMonitor : function () {
        var num = 0;
        for (var i = 0; i < fruit.num; i++){
            if(this.alive[i]){
                num++;
            }
        }
        if(num < 15){
            this.sendFruit();
            return;
        }
    },
    sendFruit : function () {
        for(var i = 0; i < fruit.num; i++){
            if(!this.alive[i]){
                this.born(i);
                return;
            }
        }
    },
    dead : function (i) {
        this.alive[i] = false;
    }
}

